
#include "bsp.h"
#include "bsp_button.h"


#define TAG "BUTTON"

#define BUTTON_PRESS_TIME  3



static u8 g_key_vlaue = UIKEY_NONE;

static uint8_t Key_select = 1;

extern SSD1306_t dev;

extern uint8_t rssi_count;

char ShowChar[20];

espnow_addr_t ESPNOW_ADDR_DST={0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF};



//成功==1 失败==0
u8 Device_SetInfo(void)
{
    esp_err_t ret  = ESP_OK;
    size_t size    = 0;
    uint8_t *data  = ESP_CALLOC(1, ESPNOW_DATA_LEN);

    espnow_frame_head_t frame_head = {
        .retransmit_count = 5,
        .broadcast        = true,
    };

    data[0] = HEAD_ONE;
    data[1] = HEAD_TWO;
    data[2] = COLTD_ONE;
    data[3] = COLTD_TWO;
    data[4] = PROTOCOL_TYPE_TWO;
    data[5] = 0;
    data[6] = 0;
    data[7] = 0;
    data[8] = PROTOCOL_TYPE_TWO_CMD_ONE;
    data[9] = RET_FLAG_NO;
    data[10] = 3;//datalen
    data[11] = App.Tosop_Edu_Channel;//通道
    data[12] = App.Tosop_Edu_Row;//行数
    data[13] = App.Tosop_Edu_column;//列数
    

    size = 14;

    if(rssi_count >=3)
    {
        ret = espnow_send(ESPNOW_TYPE_DATA, ESPNOW_ADDR_DST, data, size, &frame_head, portMAX_DELAY);
        //ESP_ERROR_CONTINUE(ret != ESP_OK, "<%s> espnow_send", esp_err_to_name(ret));
        rssi_count=0;
        ESP_FREE(data);
        return 1;
    } 
    
    ESP_FREE(data);
    return 0;
}



static void button_tap_cb1(void* arg)
{
    g_key_vlaue = UIKEY_OK;
    ESP_LOGI(TAG, "button1 release== (%s)", (char *)arg); 
    /*
    LED1_ON;
    //rssi_count=0;//接收清零
    
    ///sprintf(&ShowChar[0], "%2d", rssi_count);
    ///ssd1306_display_text(&dev, 2, ShowChar, strlen(ShowChar), false);

   
    Key_select++;
    if(Key_select>3)
    {
        Key_select=3; 
    }
    ESP_LOGI(TAG, "Key_select=%d",Key_select); 


    switch(Key_select)
    {
        case 1:
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], "<%03d> %03d  %03d ", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);

        break;
        case 2:
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], " %03d <%03d> %03d ", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);

        break;
        case 3:
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], " %03d  %03d <%03d>", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
  
        break;
        default:
        break;
    } 

    LED1_OFF;
    */
}

static void button_tap_cb2(void* arg)
{




    g_key_vlaue = UIKEY_ESC;
    ESP_LOGI(TAG, "button2 release== (%s)", (char *)arg); 

    /*
    LED1_ON;
    ESP_LOGI(TAG, "button2 release== (%s)", (char *)arg); 
    Key_select--;
    if(Key_select<1)
    {
        Key_select=1;
    }
    ESP_LOGI(TAG, "Key_select=%d",Key_select); 

    switch(Key_select)
    {
        case 1:
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], "<%03d> %03d  %03d ", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);

        break;
        case 2:
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], " %03d <%03d> %03d ", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
        break;
        case 3:
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], " %03d  %03d <%03d>", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
        break;
        default:
        break;
    } 
    LED1_OFF;
    */

}

static void button_tap_cb3(void* arg)
{

    g_key_vlaue = UIKEY_UP;
    ESP_LOGI(TAG, "button3 release== (%s)", (char *)arg);

    /*
    LED1_ON;
    ESP_LOGI(TAG, "button3 release== (%s)", (char *)arg);
    switch(Key_select)
    {
        case 1:
            App.Tosop_Edu_Channel++;
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], "<%03d> %03d  %03d ", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
        break;
        case 2:
            App.Tosop_Edu_column++;
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], " %03d <%03d> %03d ", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
        break;
        case 3:
            App.Tosop_Edu_Row++;
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], " %03d  %03d <%03d>", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
        break;
        default:
        break;
    } 

    ESP_LOGI(TAG, "1 App.Tosop_Edu_Channel==%d",App.Tosop_Edu_Channel);
    ESP_LOGI(TAG, "1 App.Tosop_Edu_column==%d",App.Tosop_Edu_column);
    ESP_LOGI(TAG, "1 App.Tosop_Edu_Row==%d",App.Tosop_Edu_Row);
    LED1_OFF;
    */

}

static void button_tap_cb4(void* arg)
{

    g_key_vlaue = UIKEY_DOWN;
    ESP_LOGI(TAG, "button4 release== (%s)", (char *)arg); 

    /*
    LED1_ON;
    ESP_LOGI(TAG, "button4 release== (%s)", (char *)arg); 
     switch(Key_select)
    {
        case 1:
            App.Tosop_Edu_Channel--;
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], "<%03d> %03d  %03d ", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
        break;
        case 2:
            App.Tosop_Edu_column--;
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], " %03d <%03d> %03d ", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
        break;
        case 3:
            App.Tosop_Edu_Row--;
            ssd1306_display_text(&dev, 5, "chan  col  row", 16, false);
            sprintf(&ShowChar[0], " %03d  %03d <%03d>", App.Tosop_Edu_Channel,App.Tosop_Edu_column,App.Tosop_Edu_Row);
            ssd1306_display_text(&dev, 6, ShowChar, strlen(ShowChar), false);
        break;
        default:
        break;
    } 

    ESP_LOGI(TAG, "2 App.Tosop_Edu_Channel==%d",App.Tosop_Edu_Channel);
    ESP_LOGI(TAG, "2 App.Tosop_Edu_column==%d",App.Tosop_Edu_column);
    ESP_LOGI(TAG, "2 App.Tosop_Edu_Row==%d",App.Tosop_Edu_Row);
    LED1_OFF;
    */

}

static void button_tap_cb5(void* arg)
{

    LED1_ON;
    ESP_LOGI(TAG, "button5 release== (%s)", (char *)arg); 
    esp_err_t ret  = ESP_OK;
    size_t size    = 0;
    uint8_t *data  = ESP_CALLOC(1, ESPNOW_DATA_LEN);

    espnow_frame_head_t frame_head = {
        .retransmit_count = 5,
        .broadcast        = true,
    };



    App.Tospo_Edu_Rssi--;
    ///sprintf(&ShowChar[0], "      %03d", App.Tospo_Edu_Rssi);
    ///ssd1306_display_text(&dev, 3, ShowChar, strlen(ShowChar), false);

    data[0] = HEAD_ONE;
    data[1] = HEAD_TWO;
    data[2] = COLTD_ONE;
    data[3] = COLTD_TWO;
    data[4] = PROTOCOL_TYPE_ONE;
    data[5] = App.Tosop_Edu_Channel;
    data[6] = App.Tosop_Edu_Row;
    data[7] = App.Tosop_Edu_column;
    data[8] = PROTOCOL_TYPE_ONE_CMD_ONE;
    data[9] = RET_FLAG_NO;
    data[10] = 1;//datalen
    data[11] = PROTOCOL_TYPE_ONE_CMD_ONE_OPEN;
    
    size = 12;

    ret = espnow_send(ESPNOW_TYPE_DATA, ESPNOW_ADDR_BROADCAST, data, size, &frame_head, portMAX_DELAY);
    //ESP_ERROR_CONTINUE(ret != ESP_OK, "<%s> espnow_send", esp_err_to_name(ret));


    ///ssd1306_display_text(&dev, 7, "       1", 8, false);
    ///vTaskDelay(1000 / portTICK_PERIOD_MS);
    ///ssd1306_display_text(&dev, 7, "        ", 8, false);

    ESP_FREE(data);
    LED1_OFF;
}

static void button_tap_cb6(void* arg)
{

    LED1_ON;
    ESP_LOGI(TAG, "button6 release== (%s)", (char *)arg); 
    esp_err_t ret  = ESP_OK;
    size_t size    = 0;
    uint8_t *data  = ESP_CALLOC(1, ESPNOW_DATA_LEN);

    espnow_frame_head_t frame_head = {
        .retransmit_count = 5,
        .broadcast        = true,
    };


    App.Tospo_Edu_Rssi++;
    ///sprintf(&ShowChar[0], "      %03d", App.Tospo_Edu_Rssi);
    ///ssd1306_display_text(&dev, 3, ShowChar, strlen(ShowChar), false);

    data[0] = HEAD_ONE;
    data[1] = HEAD_TWO;
    data[2] = COLTD_ONE;
    data[3] = COLTD_TWO;
    data[4] = PROTOCOL_TYPE_ONE;
    data[5] = App.Tosop_Edu_Channel;
    data[6] = App.Tosop_Edu_Row;
    data[7] = App.Tosop_Edu_column;
    data[8] = PROTOCOL_TYPE_ONE_CMD_ONE;
    data[9] = RET_FLAG_NO;
    data[10] = 1;//datalen
    data[11] = PROTOCOL_TYPE_ONE_CMD_ONE_CLOSE;
    
    size = 12;

    ret = espnow_send(ESPNOW_TYPE_DATA, ESPNOW_ADDR_BROADCAST, data, size, &frame_head, portMAX_DELAY);
    //ESP_ERROR_CONTINUE(ret != ESP_OK, "<%s> espnow_send", esp_err_to_name(ret));
    ///ssd1306_display_text(&dev, 7, "       0", 8, false);
    ///vTaskDelay(1000 / portTICK_PERIOD_MS);
    ///ssd1306_display_text(&dev, 7, "        ", 8, false);
    ESP_FREE(data);

    LED1_OFF;
}



static void button_press_long_cb1(void* arg)//右键
{

    ESP_LOGI(TAG, "button_press_long_cb1 ==(%s)", (char *)arg); 


   


    /*

    Key_select--;//松开后这个值还是会加1的
    esp_err_t ret  = ESP_OK;
    size_t size    = 0;
    uint8_t *data  = ESP_CALLOC(1, ESPNOW_DATA_LEN);

    espnow_frame_head_t frame_head = {
        .retransmit_count = 5,
        .broadcast        = true,
    };

    data[0] = HEAD_ONE;
    data[1] = HEAD_TWO;
    data[2] = COLTD_ONE;
    data[3] = COLTD_TWO;
    data[4] = PROTOCOL_TYPE_TWO;
    data[5] = 0;
    data[6] = 0;
    data[7] = 0;
    data[8] = PROTOCOL_TYPE_TWO_CMD_FIV;
    data[9] = RET_FLAG_NO;
    data[10] = 0;//datalen

    size = 11;

 
    ret = espnow_send(ESPNOW_TYPE_DATA, ESPNOW_ADDR_BROADCAST, data, size, &frame_head, portMAX_DELAY);

    ESP_FREE(data); 

    */


}


static void button_press_long_cb2(void* arg)//左键
{
    ESP_LOGI(TAG, "button_press_long_cb2 ==(%s)", (char *)arg); 
     g_key_vlaue = UIKEY_ESC_LONG;

    /*
    Key_select++;//松开后这个值还是会加1的
    esp_err_t ret  = ESP_OK;
    size_t size    = 0;
    uint8_t *data  = ESP_CALLOC(1, ESPNOW_DATA_LEN);

    espnow_frame_head_t frame_head = {
        .retransmit_count = 5,
        .broadcast        = true,
    };

    data[0] = HEAD_ONE;
    data[1] = HEAD_TWO;
    data[2] = COLTD_ONE;
    data[3] = COLTD_TWO;
    data[4] = PROTOCOL_TYPE_TWO;
    data[5] = 0;
    data[6] = 0;
    data[7] = 0;
    data[8] = PROTOCOL_TYPE_TWO_CMD_FOU;
    data[9] = RET_FLAG_NO;
    data[10] = 0;//datalen

    size = 11;

 
    ret = espnow_send(ESPNOW_TYPE_DATA, ESPNOW_ADDR_BROADCAST, data, size, &frame_head, portMAX_DELAY);
    //ESP_ERROR_CONTINUE(ret != ESP_OK, "<%s> espnow_send", esp_err_to_name(ret));

    ESP_FREE(data); 

    */
}


static void button_press_long_cb3(void* arg)
{
    ESP_LOGI(TAG, "button_press_long_cb3 ==(%s)", (char *)arg);
}

static void button_press_long_cb4(void* arg)
{
   ESP_LOGI(TAG, "button_press_long_cb4 ==(%s)", (char *)arg);
}


static void button_press_long_cb5(void* arg)//注册函数  
{

    ///LED2_ON;
    ESP_LOGI(TAG, "button_press_long_cb5 ==(%s)", (char *)arg);
    /*
    App.Tospo_Send_count=0;
    App.Tospo_Recv_count=0;
    
    Tospo_DataBase_Save();
    ESP_LOGI(TAG, "Tospo_DataBase_Save OK!!!");

    //Key_select--;//松开后这个值还是会加1的
    esp_err_t ret  = ESP_OK;
    size_t size    = 0;
    uint8_t *data  = ESP_CALLOC(1, ESPNOW_DATA_LEN);

    espnow_frame_head_t frame_head = {
        .retransmit_count = 5,
        .broadcast        = true,
    };

    data[0] = HEAD_ONE;
    data[1] = HEAD_TWO;
    data[2] = COLTD_ONE;
    data[3] = COLTD_TWO;
    data[4] = PROTOCOL_TYPE_TWO;
    data[5] = 0;
    data[6] = 0;
    data[7] = 0;
    data[8] = PROTOCOL_TYPE_TWO_CMD_ONE;
    data[9] = RET_FLAG_NO;
    data[10] = 3;//datalen
    data[11] = App.Tosop_Edu_Channel;//通道
    data[12] = App.Tosop_Edu_Row;//行数
    data[13] = App.Tosop_Edu_column;//列数
    

    size = 14;

    if(rssi_count >=3)
    {
        ///ret = espnow_send(ESPNOW_TYPE_DATA, ESPNOW_ADDR_BROADCAST, data, size, &frame_head, portMAX_DELAY);

        ret = espnow_send(ESPNOW_TYPE_DATA, ESPNOW_ADDR_DST, data, size, &frame_head, portMAX_DELAY);


        //ESP_ERROR_CONTINUE(ret != ESP_OK, "<%s> espnow_send", esp_err_to_name(ret));

        //sprintf(&ShowChar[0], "Row:%03d", App.Tosop_Edu_Row);
        ///ssd1306_display_text(&dev, 7, "     Set OK", 11, false);
        ///vTaskDelay(3000 / portTICK_PERIOD_MS);
        ///ssd1306_display_text(&dev, 7, "           ", 11, false);


        rssi_count=0;
    } 
    
    ESP_FREE(data);

    LED2_OFF;
    */

}


static void button_press_long_cb6(void* arg)//删除当前值
{
    ///LED2_ON;
    ESP_LOGI(TAG, "button_press_long_cb6 ==(%s)", (char *)arg);
    //Key_select++;//松开后这个值还是会加1的

    /*
    esp_err_t ret  = ESP_OK;
    size_t size    = 0;
    uint8_t *data  = ESP_CALLOC(1, ESPNOW_DATA_LEN);

    espnow_frame_head_t frame_head = {
        .retransmit_count = 5,
        .broadcast        = true,
    };

    data[0] = HEAD_ONE;
    data[1] = HEAD_TWO;
    data[2] = COLTD_ONE;
    data[3] = COLTD_TWO;
    data[4] = PROTOCOL_TYPE_TWO;
    data[5] = 0;
    data[6] = 0;
    data[7] = 0;
    data[8] = PROTOCOL_TYPE_TWO_CMD_TWO;
    data[9] = RET_FLAG_NO;
    data[10] = 0;//datalen
    
    size = 11;

    ret = espnow_send(ESPNOW_TYPE_DATA, ESPNOW_ADDR_BROADCAST, data, size, &frame_head, portMAX_DELAY);
    //ESP_ERROR_CONTINUE(ret != ESP_OK, "<%s> espnow_send", esp_err_to_name(ret));

    ESP_FREE(data);

    //sprintf(&ShowChar[0], "Row:%03d", App.Tosop_Edu_Row);
    ///ssd1306_display_text(&dev, 7, "    Clear OK", 12, false);
    ///vTaskDelay(3000 / portTICK_PERIOD_MS);
    ///ssd1306_display_text(&dev, 7, "            ", 12, false);


    //App.Tospo_Send_count=0;
    //App.Tospo_Recv_count=0;
    
    LED2_OFF;

    rssi_count=0;

    */

}


u8 uiKeyGetKey(void)
{
	return g_key_vlaue;
}

void uiKeySetKeyNone(void)
{
	g_key_vlaue = UIKEY_NONE;
}


void bsp_button_init(void)
{
    gpio_config_t io_conf;

    button_handle_t btn_handle1 = iot_button_create(BUTTON_IO_NUM_1, BUTTON_ACTIVE_LEVEL);//左键
    if (btn_handle1)
    {
        iot_button_set_evt_cb(btn_handle1, BUTTON_CB_RELEASE, button_tap_cb1, "RELEASE");
        iot_button_add_custom_cb(btn_handle1, BUTTON_PRESS_TIME, button_press_long_cb1, "3");
    }

    button_handle_t btn_handle2 = iot_button_create(BUTTON_IO_NUM_2, BUTTON_ACTIVE_LEVEL);//右键
    if (btn_handle2)
    {
        iot_button_set_evt_cb(btn_handle2, BUTTON_CB_RELEASE, button_tap_cb2, "RELEASE");
        iot_button_add_custom_cb(btn_handle2, BUTTON_PRESS_TIME, button_press_long_cb2, "3");
    }

    button_handle_t btn_handle3 = iot_button_create(BUTTON_IO_NUM_3, BUTTON_ACTIVE_LEVEL);//上键
    if (btn_handle3)
    {
        iot_button_set_evt_cb(btn_handle3, BUTTON_CB_RELEASE, button_tap_cb3, "RELEASE");
        //iot_button_add_custom_cb(btn_handle3, BUTTON_PRESS_TIME, button_press_long_cb3, "3");
    }

    button_handle_t btn_handle4 = iot_button_create(BUTTON_IO_NUM_4, BUTTON_ACTIVE_LEVEL);//下键
    if (btn_handle4)
    {
        iot_button_set_evt_cb(btn_handle4, BUTTON_CB_RELEASE, button_tap_cb4, "RELEASE");
        //iot_button_add_custom_cb(btn_handle4, BUTTON_PRESS_TIME, button_press_long_cb4, "3");
    }

    //A按键
     button_handle_t btn_handle5 = iot_button_create(BUTTON_IO_NUM_5, BUTTON_ACTIVE_LEVEL);
    if (btn_handle4)
    {
        iot_button_set_evt_cb(btn_handle5, BUTTON_CB_RELEASE, button_tap_cb5, "RELEASE");
        iot_button_add_custom_cb(btn_handle5, BUTTON_PRESS_TIME, button_press_long_cb5, "3");//长按注册
    }

    //B按键
     button_handle_t btn_handle6 = iot_button_create(BUTTON_IO_NUM_6, BUTTON_ACTIVE_LEVEL);
    if (btn_handle4)
    {
        iot_button_set_evt_cb(btn_handle6, BUTTON_CB_RELEASE, button_tap_cb6, "RELEASE"); 
        iot_button_add_custom_cb(btn_handle6, BUTTON_PRESS_TIME, button_press_long_cb6, "3");//长按删除
    }

   
    //disable interrupt
    io_conf.intr_type = GPIO_INTR_DISABLE;
    //set as output mode
    io_conf.mode = GPIO_MODE_OUTPUT;
    //bit mask of the pins that you want to set,e.g.GPIO18/19
    io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
    //disable pull-down mode
    io_conf.pull_down_en = 0;
    //disable pull-up mode
    io_conf.pull_up_en = 0;
    //configure GPIO with the given settings
    gpio_config(&io_conf);

    LED1_OFF;
    LED2_OFF;


}

